<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class m_barang extends CI_Model {

	function hapus($a) {
		$this->db->trans_start();
		$this->db->delete('tb_siswa', array('id_siswa' => $a));
		$this->db->trans_complete();
		return;
	}

	function tambah($foto=""){
		$ak = $this->input->post('nomor_ak');
		$nama = $this->input->post('nama');
		$pangkat = $this->input->post('pangkat');
		$nosenjata= $this->input->post('nosenjata'); 
		$data1 = array('nama' => $nama,'pangkat' => $pangkat, 'nomor_ak' => $ak, 'nomor_senjata' => $nosenjata, 'keterangan' => '1' , 'foto' => $foto);
		$this->db->trans_start();
		$this->db->insert('tb_siswa', $data1);
		$this->db->trans_complete();
	}

	function ambil() {
		$ambil = $this->db->get('tb_siswa');
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	function ambilbyid($id="") {
		$ambil = $this->db->get_where('tb_siswa', array('id_siswa' => $id));
		return $ambil;
	}

	function update($foto="")
	{
		$id = $this->input->post('id');
		$aka = $this->input->post('nomor_ak');
		$nama = $this->input->post('nama');
		$pangkat = $this->input->post('pangkat');
		$nosenjata= $this->input->post('nosenjata');
		$data1 = array('nama' => $nama,'pangkat' => $pangkat, 'nomor_ak' => $aka, 'nomor_senjata' => $nosenjata , 'foto' => $foto);
		//transact sql e bro
		$this->db->trans_start();
		$this->db->where('id_siswa', $id);
		$this->db->update('tb_siswa', $data1);
		$this->db->trans_complete();

	}	

	function hapus_foto($id=""){
		$data = array('foto'=> '');
		$this->db->trans_start();
		$this->db->where('id_siswa', $id);
		$this->db->update('tb_siswa', $data);
		$this->db->trans_complete();

	}

	function cek($nama, $pangkat, $nomor_ak, $nosenjata){
		$this->db->where('nama', $nama);
		$this->db->where('pangkat', $pangkat);
		$this->db->where('nomor_ak', $nomor_ak);
		$this->db->where('nomor_senjata', $nosenjata);
		$cek_hasil = $this->db->get('tb_siswa');
		if (count($cek_hasil->result()) > 0) {
			foreach ($cek_hasil->result_array() as $data) {
				$hasil[] = $data;
			}
			return true;
		}else {
			return false;
		}
	}

	function getbyid($nomor_ak)
	{
		$this->db->where('nomor_ak', $nomor_ak);
		$cek_hasil = $this->db->get('tb_siswa');
		if (count($cek_hasil->result()) > 0) {
			foreach ($cek_hasil->result_array() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}else {
			return false;
		}

	}



	function ceklengkap($nama, $pangkat, $nomor_ak, $nosenjata)
	{
		$this->db->where('nama', $nama);
		$this->db->where('pangkat', $pangkat);
		$this->db->where('nomor_ak', $nomor_ak);
		$this->db->where('nomor_senjata', $nosenjata);
		$cek_hasil = $this->db->get('tb_siswa');
		if (count($cek_hasil->result()) > 0) {
			foreach ($cek_hasil->result_array() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}else {
			return false;
		}
	}

	function ubahketerangan($nomor_ak, $keterangan){
		$this->db->trans_start();
		$this->db->where('nomor_ak', $nomor_ak);
		$this->db->update('tb_siswa', array('keterangan' => $keterangan));
		$this->db->trans_complete();
		$this->db->trans_start();
		$this->db->where('nomor_ak', $nomor_ak);
		$this->db->where('keterangan', $keterangan);
		$cek = $this->db->get('tb_siswa');
		if (count($cek->result()) > 0) {
			foreach ($cek->result_array() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}else {
			return false;
		}
		$this->db->trans_complete();
	}
}